package controllers.pc;

import models.System.Systems.Imgs;
import models.System.Systems.QR_code;
import models.System.Systems.SystemModels;
import models.System.Headline.code;
import models.System.Systems.folder;
import models.SystemModels.*;
import models.interfaces.Models;
import net.sf.json.JSONObject;
import play.Logger;
import play.cache.Cache;
import play.libs.Images;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018-06-18.
 */
public class pc_index extends Logo {

    public static Models model = new SystemModels();

    public static void pc_index(){


        render();
    }

    public static void captcha(String id) {
        Images.Captcha captcha = Images.captcha();
        String code = captcha.getText("#E4EAFD");
        Cache.set(id, code, "10mn");//缓存在服务端
        renderBinary(captcha);
    }

    public static void pc_logo(){
        render();
    }

    public static void pc_Update(File file){
      String path =  model.Imgs(file);
      renderJSON(path);
    }

    /**
     * @创建系统文件夹，创建完之后创建个人文件夹
     * @检查用户是否有用户的文件夹信息
     * @如果不存在直接添加到数据库里边
     * @存在之后返回书据
     */
    public static void pc_folder(){
        session.put("name","admin");
        String name = session.get("name");
        String file = params.get("name");
        Logger.info(file);
        if (name==null){
            renderJSON(0);
        }else{
            int code =models.System.Systems.folder.folderCreate(name,file);
            Logger.info(code+"");
            renderJSON(1);
        }
    }

    public static void pc_folders(){
        String name = code.fileName;
        String msg = code.Images;
        List<SystemDesign> systemDesignList = SystemDesign.findAll();
        List<folders> list = folders.find("userId", folder.userId(session.get("name"))).fetch();
        Logger.info(list.size()+"");
        render(name,msg,systemDesignList,list);
    }

    /**
     * @新建文件夹信息
     * @如果当前文件夹没有名字，那么就可以起一个默认名字
     * @如果有文件夹那么追加（1）
     */
    public static void pc_folders_create(){
        session.put("name","admin");
        String name = session.get("name");
        List<SystemUser> systemUsers = SystemUser.find("UserName",name).fetch();
        if (systemUsers.isEmpty()){
            renderJSON(0);
        }else {
            List<folders> list = folders.find("userId", systemUsers.get(0).Id.toString()).fetch();
            if (list.isEmpty()){
                renderJSON(0);
            }else if(!list.isEmpty()){

              JSONObject json = new JSONObject();
              json.put("code",1);
              json.put("fileNmae",code.fileName);
              renderJSON(json);
            }
        }


    }

    public static void pc_files(){
      render();
    }

    public static void pc_file(){
        String attachment =params.get("url");
       Logger.info(attachment);
    }

    public static void index(){
        render();
    }

    public static void personal(){
        render();
    }

    public static void modify(){
        render();
    }

    /**
     * 自定义404错误，代替系统出错
     */
    public static void personals(){
        try {
            if (!session.get("code").isEmpty()){
                render();
            }
        }catch (Exception ex){
            pc_error.pageEx = ex;
            pc_error.page();
        }
    }

    public static void header(){
        render();
    }

    public static void expenditure(){
        List<SystemExpenditure> meun = new ArrayList<SystemExpenditure>();
        int size = SystemExpenditure.findAll().size();
        int number = size/5+1;
        int pageSize=0;
        try{

            pageSize = Integer.parseInt(params.get("pageSize"));
            meun= SystemExpenditure.all().from(pageSize*5).fetch((pageSize+1)*5);
            int i = meun.size();
            Logger.info(meun.size()+"");
            render(meun,size,number,pageSize,i);
        }catch (Exception ex){
            meun = SystemExpenditure.all().from(0).fetch(5);
            int i = meun.size();
            render(meun,size,number,pageSize,i);
        }

        render();
    }
}
